import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/utils/date_tools.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

enum DateTimeType {
  single,
  range,
}


void showMultiDatePicker({
  required BuildContext context,
  void Function(List<String>)? onchange,
  DateTime? minimumDate,
  DateTime? initialDateTime,
  DateTime? maximumDate,
  CupertinoDatePickerMode? mode,
  String? format,
  DateTimeType? type,
  DateTime? startDate,
  DateTime? endDate,
}) {

  int activeIndex = 0;
  DateTime _startDate = startDate ?? DateTime.now();
  DateTime _endDate = endDate ?? DateTime.now();
  DateTime dateTime = initialDateTime ?? _startDate;
  bool show = true;

  showCupertinoModalPopup(
    context: context,
    builder: (BuildContext context) {
      return StatefulBuilder(builder: (mDialogContext, customState) {
        return Container(
          height: type == DateTimeType.single ? 246 : 300,
          padding: const EdgeInsets.only(top: 6.0),
          margin: EdgeInsets.only(
            bottom: MediaQuery.of(context).viewInsets.bottom,
          ),
          color: CupertinoColors.systemBackground.resolveFrom(context),
          child: SafeArea(
            top: false,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      GestureDetector(
                        onTap:() {
                          Navigator.pop(context);
                        },
                        child: const Text("取消"),
                      ),
                      GestureDetector(
                        onTap:() {
                          // DateTools.formatTime(dateTime.millisecondsSinceEpoch, format: format ?? "{y}-{m}-{d}")
                          if (_startDate.isAfter(_endDate)) {
                            EasyLoading.showInfo("开始时间不能大于结束时间");
                            return;
                          }
                          if (onchange != null) onchange([DateTools.formatYMD(_startDate), DateTools.formatYMD(_endDate)]);
                          Navigator.pop(context);
                        },
                        child: const Text("确定", style: TextStyle(color: ThemeVars.colorPrimary),),
                      ),
                    ],
                  ),
                ),
                if (type == DateTimeType.range) SizedBox(
                  // height: 36,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          customState(() {
                            show = false;
                            activeIndex = 0;
                            dateTime = _startDate;
                          });
                          Future.delayed(const Duration(milliseconds: 100), () {
                            customState(() {
                              show = true;
                            });
                          });
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 3, horizontal: 12),
                          decoration: BoxDecoration(
                              border: Border(bottom: BorderSide(color: activeIndex == 0 ? ThemeVars.colorPrimary : ThemeVars.colorBorder))
                          ),
                          child: Text(
                            DateTools.formatYMD(_startDate),
                            style: TextStyle(color: activeIndex == 0 ? ThemeVars.colorPrimary : ThemeVars.colorText, fontSize: 16),),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 12),
                        child: Text("至"),
                      ),
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          customState(() {
                            show = false;
                            dateTime = _endDate;
                            activeIndex = 1;
                          });
                          Future.delayed(const Duration(milliseconds: 100), () {
                            customState(() {
                              show = true;
                            });
                          });
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 3, horizontal: 12),
                          decoration: BoxDecoration(
                              border: Border(bottom: BorderSide(color: activeIndex == 1 ? ThemeVars.colorPrimary : ThemeVars.colorBorder))
                          ),
                          child: Text(
                            DateTools.formatYMD(_endDate),
                            style: TextStyle(color: activeIndex == 1 ? ThemeVars.colorPrimary : ThemeVars.colorText, fontSize: 16),),
                        ),
                      ),
                    ],
                  ),
                ),
                if (show) SizedBox(
                  width: MediaQuery.of(context).size.width,
                  height: 216,
                  child: activeIndex == 0 ? CupertinoDatePicker(
                    mode: mode ?? CupertinoDatePickerMode.date,
                    initialDateTime: dateTime,
                    minimumDate: minimumDate ?? DateTime(1970, 1, 1),
                    maximumDate: maximumDate ?? DateTime(2035, 12, 31),
                    minimumYear: 1970,
                    maximumYear: 2035,
                    use24hFormat: true,
                    // This shows day of week alongside day of month
                    showDayOfWeek: false,
                    dateOrder: DatePickerDateOrder.ymd,

                    // This is called when the user changes the date.
                    onDateTimeChanged: (DateTime newDate) {
                      dateTime = newDate;
                      customState(() {
                        activeIndex == 0 ? _startDate = newDate : _endDate = newDate;
                      });
                    },
                  ) : CupertinoDatePicker(
                    mode: mode ?? CupertinoDatePickerMode.date,
                    initialDateTime: _endDate,
                    minimumDate: minimumDate ?? DateTime(1970, 1, 1),
                    maximumDate: maximumDate ?? DateTime(2035, 12, 31),
                    minimumYear: 1970,
                    maximumYear: 2035,
                    use24hFormat: true,
                    // This shows day of week alongside day of month
                    showDayOfWeek: false,
                    dateOrder: DatePickerDateOrder.ymd,

                    // This is called when the user changes the date.
                    onDateTimeChanged: (DateTime newDate) {
                      dateTime = newDate;
                      customState(() {
                        activeIndex == 0 ? _startDate = newDate : _endDate = newDate;
                      });
                    },
                  ),
                ),
              ],
            ),
          ),
        );
      });
    }
  );
}